package cn.hncj.lyc.controller.front;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.hncj.lyc.bean.AdvertisementManagement;
import cn.hncj.lyc.bean.GoodsInfo;
import cn.hncj.lyc.bean.UserAdmin;
import cn.hncj.lyc.bean.UserInfo;
import cn.hncj.lyc.service.AdvertisementManagementService;
import cn.hncj.lyc.service.GoodsInfoService;
import cn.hncj.lyc.service.SearchGoodsInfo;
import cn.hncj.lyc.service.UserInfoService;
import cn.hncj.lyc.service.UserService;
import cn.hncj.lyc.service.impl.UserInfoRegServiceImpl;
import cn.hncj.lyc.utils.Key;
import cn.hncj.lyc.utils.PageBean;

/**
 * 商城前台的控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/")
public class FrontController {
	
	@Autowired
	private GoodsInfoService goodsInfoService;
	
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private SearchGoodsInfo SeGoodsInfo;
	 @Autowired
	private AdvertisementManagementService  advertManSer;
	 @Autowired
	 private UserInfoRegServiceImpl userInfoRegServiceImpl;
	
	/*
	 * 查询商品
	 */
	@RequestMapping("search")
	public String search(GoodsInfo goodsName,AdvertisementManagement advertisementmanagement,Model model){
		try {
		List<AdvertisementManagement> advertman= advertManSer.showAdvertisement(advertisementmanagement);
  		 // model.addAttribute("advertman", advertman);
  		  AdvertisementManagement  advertone=new AdvertisementManagement();
  		  AdvertisementManagement  adverttwo=new AdvertisementManagement();
  		  AdvertisementManagement  advertthree=new AdvertisementManagement();
  	      advertone.setAdvertisementUrl(advertman.get(0).getAdvertisementUrl());
  		  adverttwo.setAdvertisementUrl(advertman.get(1).getAdvertisementUrl());
  		  advertthree.setAdvertisementUrl(advertman.get(2).getAdvertisementUrl());
  		  model.addAttribute("advertone", advertone.getAdvertisementUrl());
  		  model.addAttribute("adverttwo", adverttwo.getAdvertisementUrl());
  		  model.addAttribute("advertthree", advertthree.getAdvertisementUrl());
		  List<GoodsInfo> seList=SeGoodsInfo.searchGoodsInfo(goodsName);
		  model.addAttribute("seList",seList);//保存查询条件，
	    } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//model.addAttribute("result", "展示失败");
		}
		return "front/searchgoodsinfo_list";
	}
	
	/**
	 * 展示商城首页
	 * @return
	 */
	@RequestMapping("index")
	//@Cacheable(cacheNames="lyc",key="#p0")
	public String index(AdvertisementManagement advertisementmanagement, Model model){
		 try {
   		  List<AdvertisementManagement> advertman= advertManSer.showAdvertisement(advertisementmanagement);
	  		 // model.addAttribute("advertman", advertman);
	  		  AdvertisementManagement  advertone=new AdvertisementManagement();
	  		  AdvertisementManagement  adverttwo=new AdvertisementManagement();
	  		  AdvertisementManagement  advertthree=new AdvertisementManagement();
	  	      advertone.setAdvertisementUrl(advertman.get(0).getAdvertisementUrl());
	  		  adverttwo.setAdvertisementUrl(advertman.get(1).getAdvertisementUrl());
	  		  advertthree.setAdvertisementUrl(advertman.get(2).getAdvertisementUrl());
	  		  model.addAttribute("advertone", advertone.getAdvertisementUrl());
	  		  model.addAttribute("adverttwo", adverttwo.getAdvertisementUrl());
	  		  model.addAttribute("advertthree", advertthree.getAdvertisementUrl());
	  		  System.out.println(advertone.getAdvertisementUrl());
	  		  System.out.println(adverttwo.getAdvertisementUrl());
	  		  System.out.println(advertthree.getAdvertisementUrl());	  			  		
	  		  List<GoodsInfo> list = goodsInfoService.getFrontGoodsInfoList(new GoodsInfo());
			  model.addAttribute("list", list);
			//查询商品信息 
   		    //model.addAttribute("result", "展示成功");
	  		} catch (Exception e) {
	  			// TODO Auto-generated catch block
	  			e.printStackTrace();
	  			//model.addAttribute("result", "展示失败");
	  		}
	  		return "front/index";
	}
	
	/**
	 * 查看商品详情
	 * @return
	 */
	@RequestMapping("goodsinfo/{goodsId}")
	public String getGoodsInfo(@PathVariable String goodsId,Model model){
		GoodsInfo goodsInfo = new GoodsInfo();
		goodsInfo.setGoodsId(goodsId);
		GoodsInfo pgoodsInfo = goodsInfoService.getGoodsInfo(goodsInfo);
		model.addAttribute("g", pgoodsInfo);
		return "front/goodsinfo";
	}
	
	/**
	 * 商品加入购物车
	 * @param goodsId
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping("cart/{goodsId}")
	public String addToCart(@PathVariable String goodsId,Model model,HttpSession session){
		Map<String , GoodsInfo> cart = (Map<String, GoodsInfo>)session.getAttribute("cart");
		if(cart == null){
			cart = new HashMap<String, GoodsInfo>();
			session.setAttribute("cart", cart);
		}
		GoodsInfo goodsInfo = new GoodsInfo();
		goodsInfo.setGoodsId(goodsId);
		goodsInfo = goodsInfoService.getGoodsInfo(goodsInfo);
		cart.put(goodsId+goodsInfo.getGoodsName(), goodsInfo);
		return "front/shoppingcart";
	}
	/**
	 * 删除商品
	 * @param keys
	 * @param session
	 * @return
	 */
	@RequestMapping("cart/delete")
	public String cartDelete(String[] keys,HttpSession session){
		Map<String , GoodsInfo> cart = (Map<String, GoodsInfo>)session.getAttribute("cart");
		for(String key : keys){
			System.out.println("key = " + key);
			cart.remove(key);
		}
		return "front/shoppingcart";
	}
	
	/**
	 * 显示购物车
	 * @return
	 */
	@RequestMapping("cart/show")
	public String cartShow(){
		return "front/shoppingcart";
	}
	/*
	 * 用户注册页面
	 */	
	 @RequestMapping("login")
	    public String login(){
	        return "user/login";
	    }
	
	/**
	 * 用户登陆页面
	 * @return
	 */
	@RequestMapping(value = "register")
    public String register(){
        return "user/register";
    }
//	@RequestMapping("login")
//	public String login(){
//		return "login";
//	}
	
//	@RequestMapping("dologin")
//	public String doLogin(UserInfo userInfo,HttpSession session ,AdvertisementManagement advertisementmanagement,Model model){
//		UserInfo loginuser = userInfoService.doLogin(userInfo);
//		if(loginuser != null){
//			
//			session.setAttribute("loginuser", loginuser);
//			return index(advertisementmanagement,model);
//		}else{
//			//model.addAttribute("logininfo", "账号或密码错误！");
//			return "front/login";
//		}
//	}
	
	

//	@RequestMapping("loadReg")
//	public String loadReg(){
//		return "register";
//	}
//	@RequestMapping("reg")
//	public String reg(UserInfo user,Model model) throws Exception {						
//		    user.setUserType("2");			
//			userInfoService.addUserInfo(user);
//			model.addAttribute("info", "恭喜你，注册成功！请登录!");
//			return "login";
//		
//	}
	//@RequestMapping("reg")
//	@RequestMapping(value="/sendsms/{mobile}",method= RequestMethod.POST)
//	public String smsReg(@PathVariable String mobile,UserInfo user, Model model) throws Exception {						
//		    user.setUserType("2");	
//		    userInfoRegServiceImpl.sendSms(mobile);
//			userInfoService.addUserInfo(user);
//			model.addAttribute("info", "恭喜你，注册成功！请登录!");
//			return "login";		      		        		 
//		
//	}
}
